<template>
  <div class="my-container">
    <!-- 已登录 -->
    <div class="userInfo login_header" v-if="user">
      <!-- 基本信息 -->
      <div class="base">
        <div class="left">
          <van-image fit="cover" :src="userInfo.photo" class="avatar" round />
          <span class="name">{{ userInfo.name }}</span>
        </div>
        <div class="right">
          <van-button type="default" size="mini" round to="/profile"
            >编辑资料</van-button
          >
        </div>
      </div>
      <!-- 粉丝、关注 -->
      <div class="data">
        <div class="data-item">
          <span>{{ userInfo.art_count }}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{ userInfo.follow_count }}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{ userInfo.fans_count }}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{ userInfo.like_count }}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>
    <!-- 未登录 -->
    <div class="header not-login" v-else>
      <!-- 点击 登录/注册 按钮，通过路由管理器 切换到响应组件 -->
      <div class="login-btn" @click="$router.push('./login')">
        <img class="mobile-img" src="~@/assets/mobile.png" />
        <span class="text">登录/注册</span>
      </div>
    </div>
    <!-- 宫格 -->
    <van-grid class="nav-grid" :column-num="2" clickable>
      <van-grid-item text="收藏" to="/collection">
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span class="text" slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item text="历史" to="/history">
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span class="text" slot="text">历史</span>
      </van-grid-item>
    </van-grid>
    <div class="grid-nav"></div>
    <van-cell title="消息通知" is-link url="" />
    <van-cell title="实名认证" is-link url="" />
    <van-cell title="用户反馈" is-link url="" />
    <van-cell title="小智同学" is-link url="" />
    <van-cell title="系统设置" is-link url="" />
    <van-cell
      class="logout-cell"
      title="退出登录"
      center
      v-if="user"
      @click="logout"
    />
  </div>
</template>

<script>
// 用户数据   用户收藏列表
import { getUserInfo } from '@/api/user'
// 导入 vuex 的state映射方法，用来将vuex.state里的属性 映射成当前组件的计算属性，以便于组件中访问 --> $store.state.user -> user
import { mapState } from 'vuex'
export default {
  name: 'my',

  data () {
    return {
      userInfo: {}
    }
  },

  async created () {
    // 如果仓库中有用户信息，则发送请求
    if (this.$store.state.user) {
      try {
        // 请求当前登录用户的信息
        const res = await getUserInfo()
        console.log('my', res)
        // 将返回的数据赋到data中，方便使用
        this.userInfo = res.data.data
      } catch (error) {
        console.log(error.message)
      }
    }
  },

  mounted () {},

  methods: {
    // 退出按钮
    logout () {
      // 确认弹框提示
      this.$dialog
        .confirm({
          title: '系统提示捏~(￣▽￣)~*',
          message: '确认要退出嘛＞︿＜'
        })
        .then(() => {
          // 删除 vuex仓库里的 token，删除localStorage里的 token
          this.$store.commit('setUserToken', null)
          // 退出提示
          this.$toast.success('退出成功')
        })
        .catch(() => {
          // on cancel
        })
    }
  },

  computed: {
    // 将vuex仓库中state中的 user 映射成一个 计算属性
    ...mapState(['user'])
  }

  // activated () {
  //   console.log('activated->my')
  // },
  // deactivated () {
  //   console.log('deactivated->my')
  // }
}
</script>

<style lang="less" scoped>
.my-container {
  // 未登录
  > .header {
    height: 361px;
    background: url(~@/assets/banner.png) no-repeat;
    background-size: cover;
    display: flex;
    justify-content: center;
    align-items: center;
    .mobile-img {
      width: 132px;
      height: 132px;
      margin-bottom: 15px;
    }
  }
  .not-login {
    .login-btn {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .text {
        font-size: 28px;
        color: #fff;
      }
    }
  }

  // 已登录
  .login_header {
    width: 750px;
    height: 401px;
    background: url(~@/assets/02.jpg);
    background-size: cover;
  }
  .userInfo {
    .base {
      height: 231px;
      padding: 76px 32px 23px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      box-sizing: border-box;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 132px;
          height: 132px;
          border: 2px solid #fff;
        }
        .name {
          font-size: 29px;
          margin-left: 22px;
        }
      }
    }
    .data {
      display: flex;
      justify-content: space-around;
      .data-item {
        height: 130px;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        color: #000000;
        font-weight: bolder;
        span:nth-child(1) {
          font-size: 36px;
          margin-bottom: 15px;
        }
        span:nth-child(2) {
          font-size: 23px;
        }
      }
    }
  }
  // 宫格
  .nav-grid {
    background: #fff;
    .toutiao {
      font-size: 45px;
    }
    .toutiao-shoucang {
      color: #eb5253;
    }
    .toutiao-lishi {
      color: #ff9d1d;
    }
    .text {
      color: #000;
      margin-top: 8px;
      font-size: 28px;
    }
  }
  // 按钮列表
  .logout-cell {
    height: 100px;
    text-align: center;
    color: #eb5253;
    margin: 10px 0;
    font-size: 30px;
  }
}
</style>
